﻿package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import GameManager;
	
	public class bullet extends MovieClip
	{
		private var _direction:String;
		private var _power:int;
		private var _GM:GameManager;
		
		public function bullet(bulletDirection:String,bulletPower:int)
		{
			//listen for when the character is added to the play area
			addEventListener(Event.ADDED_TO_STAGE, onStage);
			_direction = bulletDirection;
			_power = bulletPower;
		}
		private function onStage(e:Event){
			//remove the added listener
			removeEventListener(Event.ADDED_TO_STAGE, onStage);
			
			_GM = parent as GameManager;
			
			//register listeners for update stuff if we decouple the system more
		}
		
		public function moveBullet(){
			//if the there is something in the bullet variable then move it
				switch(_direction)
				{
					case "up":
					y-= _power;
					break;

					case "down":
					y+= _power;
					break;
					
					case "right":
					x+= _power;
					break;

					case "left":
					x-= _power;
					break;
				}
				
				if(_GM.mazeClip.hitTestPoint(x,y,true))
				{	
					_GM.removeChild(this);
					_GM.bulletRef = null;
					return
				}
				
				//firesClip
				if(_GM.firesClip.hitTestPoint(x,y,true))
				{	
					_GM.removeChild(this);
					_GM.bulletRef = null;
					return
				}			
				//exitclip
				if(_GM.exitClip.hitTestPoint(x,y,true))
				{	
					_GM.removeChild(this);
					_GM.bulletRef = null;
					return
				}
				for each (var guard in _GM.guards)
				{
					if(guard.hitTestPoint(x,y,true))
					{	
						_GM.removeChild(this);
						_GM.bulletRef = null;
						//guard.visible = false;
						_GM.removeChild(guard);
						return
					}
				}
					
			
		}
	}
}